package com.yunxi.module.system.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import com.yunxi.framework.common.enums.UserTypeEnum;
import com.yunxi.framework.common.exception.util.ServiceExceptionUtil;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.datapermission.core.annotation.DataPermission;
import com.yunxi.framework.security.core.util.SecurityFrameworkUtils;
import com.yunxi.module.infra.enums.ErrorCodeConstants;
import com.yunxi.module.system.convert.user.UserConvert;
import com.yunxi.module.system.model.dept.DeptDO;
import com.yunxi.module.system.model.dept.PostDO;
import com.yunxi.module.system.model.permission.RoleDO;
import com.yunxi.module.system.model.social.SocialUserDO;
import com.yunxi.module.system.model.user.AdminUserDO;
import com.yunxi.module.system.service.dept.DeptService;
import com.yunxi.module.system.service.dept.PostService;
import com.yunxi.module.system.service.permission.PermissionService;
import com.yunxi.module.system.service.permission.RoleService;
import com.yunxi.module.system.service.social.SocialUserService;
import com.yunxi.module.system.service.user.AdminUserService;
import com.yunxi.module.system.vo.admin.user.profile.UserProfileRespVO;
import com.yunxi.module.system.vo.admin.user.profile.UserProfileUpdatePasswordReqVO;
import com.yunxi.module.system.vo.admin.user.profile.UserProfileUpdateReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 管理后台 - 用户个人中心
 * </p>
 *
 * @author lidy
 * @since 2024-05-23
 */
@Tag(name = "管理后台 - 用户个人中心")
@RestController
@RequestMapping("/system/user/profile")
@Validated
@Slf4j
public class UserProfileController {

    @Resource
    private AdminUserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @Resource
    private DeptService deptService;

    @Resource
    private PostService postService;

    @Resource
    private SocialUserService socialUserService;

    // 这样写是为了解决 uni-app 不支持 put 上传文件
    @RequestMapping(value = "/updateAvatar", method = {RequestMethod.POST, RequestMethod.PUT})
    @Operation(summary = "上传用户个人头像")
    public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.FILE_IS_EMPTY);
        }
        // 上传头像，返回头像地址
        String avatarUrl = userService.updateUserAvatar(SecurityFrameworkUtils.getLoginUserId(), file.getInputStream());
        return CommonResult.success(avatarUrl);
    }

    @PutMapping("/updatePassword")
    @Operation(summary = "修改用户个人密码")
    public CommonResult<Boolean> updateUserProfilePassword(@Valid @RequestBody UserProfileUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(SecurityFrameworkUtils.getLoginUserId(), reqVO);
        return CommonResult.success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "修改用户个人信息")
    public CommonResult<Boolean> updateUserProfile(@Valid @RequestBody UserProfileUpdateReqVO reqVO) {
        userService.updateUserProfile(SecurityFrameworkUtils.getLoginUserId(), reqVO);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得登录用户信息")
    @DataPermission(enable = false) // 关闭数据权限，避免只查看自己时，查询不到部门
    public CommonResult<UserProfileRespVO> getUserProfile() {
        // 获得用户基本信息
        AdminUserDO user = userService.getUser(SecurityFrameworkUtils.getLoginUserId());
        // 获得用户角色
        List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(user.getId()));

        // 获得部门信息
        DeptDO deptDO = user.getDeptId() != null ? deptService.getDept(user.getDeptId()) : null;
        // 获得岗位信息
        List<PostDO> postDOList = CollUtil.isNotEmpty(user.getPostIds()) ? postService.getPostList(user.getPostIds()) : null;
        // 获得社交用户信息
        List<SocialUserDO> socialUserDOList = socialUserService.getSocialUserList(user.getId(), UserTypeEnum.ADMIN.getValue());

        return CommonResult.success(UserConvert.INSTANCE.convert(user, userRoles, deptDO, postDOList, socialUserDOList));
    }

}

